#!/bin/bash

# Copyright (c) 2022 FuTeng
# FuYao-RepoPackageTest-System is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.

# #################################################
# @Author    :   futeng
# @Contact   :   lanshaa123@163.com
# @Desc      :   扶摇docker版函数总脚本
# @Note      :   宿主机需要安装docker
# #################################################

source ./parameters/global-variable.sh

# base源软件包安装测试
function baseInstall(){
	# base源软件包安装测试
	ROBOTS_NUM=$1
	checkDockerImage
	echo -e "\033[41;37m 即将测试【base源】软件包【安装测试】 \033[0m"
	echo -e "\033[42;37m 机器人个数 $ROBOTS_NUM \033[0m"
	# 获取被测源软件包的数目，并抓取被测源软件包列表
	getTotalPkg 1>/dev/null 2>/dev/null
	ALL_PKGS=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`
	echo -e "\033[42;37m 被测源的可安装软件包数目为： $ALL_PKGS \033[0m"
	# 切分软件包列表
	spRepoList $ROBOTS_NUM
	createTestScripts $ROBOTS_NUM -I
	bash $TEST_TEMP/run-test-all.sh  
}

# base源软件包安装卸载测试
function baseIU(){
	# base源软件包安装卸载测试
	ROBOTS_NUM=$1
	checkDockerImage
	echo -e "\033[41;37m 即将测试【base源】软件包【安装卸载测试】 \033[0m"
	echo -e "\033[42;37m 机器人个数 $ROBOTS_NUM \033[0m"
	# 获取被测源软件包的数目，并抓取被测源软件包列表
	getTotalPkg 1>/dev/null 2>/dev/null
	ALL_PKGS=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`
	echo -e "\033[42;37m 被测源的可安装软件包数目为： $ALL_PKGS \033[0m"
	# 切分软件包列表
	spRepoList $ROBOTS_NUM
	createTestScripts $ROBOTS_NUM -IU
	bash $TEST_TEMP/run-test-all.sh  
}

# 指定源软件包安装测试
function exInstall(){
	# 指定源软件包安装测试
	repoid=$1
	ROBOTS_NUM=$2
	checkDockerImage
	echo -e "\033[41;37m 即将测试【指定源】软件包【安装测试】 \033[0m"
	echo -e "\033[41;37m 注意：指定源中的软件包很可能与base源中的软件包有依赖关系，建议开启base源 \033[0m"
	echo -e "\033[42;37m 【指定源】$repoid \033[0m"
	echo -e "\033[42;37m 机器人个数 $ROBOTS_NUM \033[0m"
	# 获取被测源软件包的数目，并抓取被测源软件包列表
	getTotalPkg $repoid 1>/dev/null 2>/dev/null
	ALL_PKGS=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`
	echo -e "\033[42;37m 被测源的可安装软件包数目为： $ALL_PKGS \033[0m"
	# 切分软件包列表
	spRepoList $ROBOTS_NUM
	createTestScripts $ROBOTS_NUM -I $repoid
	bash $TEST_TEMP/run-test-all.sh  
}

# 指定源软件包安装卸载测试
function exIU(){
	# 指定源软件包安装卸载测试
	repoid=$1
	ROBOTS_NUM=$2
	checkDockerImage
	echo -e "\033[41;37m 即将测试【指定源】软件包【安装卸载测试】 \033[0m"
	echo -e "\033[41;37m 注意：指定源中的软件包很可能与base源中的软件包有依赖关系，建议开启base源 \033[0m"
	echo -e "\033[42;37m 【指定源】$repoid \033[0m"
	echo -e "\033[42;37m 机器人个数 $ROBOTS_NUM \033[0m"
	getTotalPkg $repoid 1>/dev/null 2>/dev/null
	ALL_PKGS=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`
	echo -e "\033[42;37m 被测源的可安装软件包数目为： $ALL_PKGS \033[0m"
	# 切分软件包列表
	spRepoList $ROBOTS_NUM
	createTestScripts $ROBOTS_NUM -IU $repoid
	bash $TEST_TEMP/run-test-all.sh  
}

# 帮助提示信息
function help(){
	echo -e "\033[41;37m 扶摇仓库脚本测试集（docker版） \033[0m"
	echo -e "\033[45;37m Usage: \033[0m"
	echo -e "\033[42;37m bash index.sh args \033[0m"
	echo -e "\033[45;37m Options: \033[0m"
	echo -e "\033[42;37m -ci----------------根据被测ISO创建docker镜像（测试之前的操作） --\033[0m"
	echo -e "\033[42;37m -ci iso_path/iso_name -------------------------------------\033[0m"
	echo -e "\033[45;37m -check-------------检查宿主机测试环境 ------------------------\033[0m"
	echo -e "\033[42;37m -bi----------------进行Base仓库软件包安装测试 -----------------\033[0m"
	echo -e "\033[42;37m -bi robots_number -----------------------------------------\033[0m"
	echo -e "\033[45;37m -biu---------------进行Base仓库软件包安装卸载测试 -------------\033[0m"
	echo -e "\033[45;37m -biu robots_number ----------------------------------------\033[0m"
	echo -e "\033[42;37m -exi---------------进行指定仓库软件包安装测试 -----------------\033[0m"
	echo -e "\033[42;37m -exi repoid robots_number ---------------------------------\033[0m"
	echo -e "\033[45;37m -exiu--------------进行指定仓库软件包安装卸载测试 --------------\033[0m"
	echo -e "\033[45;37m -exiu repoid robots_number --------------------------------\033[0m"
	echo -e "\033[42;37m -degrade-----------进行软件包降级测试 ------------------------\033[0m"
	echo -e "\033[45;37m -result------------查看测试结果 -----------------------------\033[0m"
	echo -e "\033[41;37m -clean-------------清除之前的测试结果 慎用！！！----------------\033[0m"
	echo -e "\033[42;37m -h-----------------展示此条帮助信息 --------------------------\033[0m"
}

# 创建身份证文件\检查身份证文件是否存在
function checkUUID(){
	if [ -f $WORK_DIR/UUID ]
	then
		echo "UUID file is exist" > /dev/null
	else
		uuidgen > $WORK_DIR/UUID
	fi 
}

# 检查被测ISO是否存在
function checkISO(){
    if [ -z $1 ]
    then
        echo -e "\033[41;37m ERROR!!! \033[0m"
        echo -e "\033[41;37m Can not find ISO $1  \033[0m"
        exit 0
    fi
    if [ -f $1 ]
    then
        echo -e "\033[42;37m Mount nfs at /mnt/nfs successful \033[0m" > /dev/null
        # no out put
        echo "####################################################################"
    else
        echo -e "\033[41;37m Oh dear! ISO file $1 does not exist! Please check the input parameters!\033[0m"
        echo -e "\033[41;37m 亲，ISO文件 $1 不存在！汝需确认一下输入的参数！ \033[0m"
        exit 0
    fi
}

# 检测宿主机docker服务的运行状态
function checkDocker(){
	systemctl status docker >/dev/null
	if [ $? -ne 0 ]
	then
		systemctl start docker > /dev/null
		if [ $? -ne 0 ]
		then
			yum install docker -y > /dev/null
			if [ $? -ne 0 ]
			then
				echo -e "\033[41;37m Can not install docker, please check your yum repository! \033[0m"
				echo -e "\033[41;37m 安装docker失败，请检查你的yum源！ \033[0m"
				exit 0
			fi
		else
			systemctl status docker > /dev/null
			if [ $? -ne 0 ]
			then
				echo -e "\033[42;37m Docker service is already running! \033[0m"
				echo -e "\033[42;37m Docker 服务已经在运行！ \033[0m"
			fi
		fi
	else
		echo -e "\033[42;37m Docker service is already running! \033[0m"
		echo -e "\033[42;37m Docker 服务已经在运行！ \033[0m"
	fi
}

# 通过被测ISO创建docker镜像 
function createDockerImage(){
	isoName=$1
	echo -e "\033[41;37m The creation of a Docker image is about to begin, which is a necessary operation before package testing !!! \033[0m"
	echo -e "\033[41;37m 即将进行docker镜像的创建，这是在包测试之前必须执行的操作! \033[0m"
	docker images | grep $I_UUID  1>/dev/null 2>/dev/null
	if [ $? -ne 0 ]
	then
		bash $SCRIPTS_DIR/create_image.sh $isoName 1>/dev/null 2>/dev/null
		echo -e "\033[42;37m Docker image repotest-$I_UUID had been created complete! \033[0m"
		echo -e "\033[42;37m Docker镜像 repotest-$I_UUID 已经创建完成！ \033[0m"
		docker images | grep REPOSITORY
		docker images | grep repotest-$I_UUID
	else
		echo -e "\033[42;37m Docker image repotest-$I_UUID has been created! You can start testing directly. \033[0m"
		echo -e "\033[42;37m Docker镜像 repotest-$I_UUID 已经被创建！可以直接开始测试 \033[0m"
	fi
}

# 检查目标docker镜像是否已经被创建
function checkDockerImage(){
	docker images | grep $I_UUID  1>/dev/null 2>/dev/null
	if [ $? -ne 0 ]
	then
		echo -e "\033[41;37m Docker image repotest-$I_UUID has not been created! \033[0m"
		echo -e "\033[41;37m Docker镜像 repotest-$I_UUID 未被创建！ \033[0m"
		exit 0
	else
		echo -e "\033[42;37m Docker镜像 repotest-$I_UUID 已经被创建！可以直接开始测试 \033[0m" > /dev/null
	fi
}

# 获取被测源中软件包总数
function getTotalPkg(){
	if [ -z "$1" ]
	then
		# get base repo pkgs
		cp -rvp $TEST_SCRIPTS_DIR/get-pkg-num.sh.base $TEST_TEMP/get-pkg-num.sh
        IMAGE_ID=$(docker images | grep $I_UUID | awk '{print $3}')
        docker create -it --name PKGNUM-$I_UUID $IMAGE_ID  /bin/bash 
        CONTAINER_ID=$(docker ps -a | grep $I_UUID | awk '{print $1}')
        docker cp $TEST_TEMP/get-pkg-num.sh $CONTAINER_ID:/root 
        docker start $CONTAINER_ID 
        docker exec -it $CONTAINER_ID /bin/bash /root/get-pkg-num.sh 
        docker cp $CONTAINER_ID:/root/repoPkgs.list $PACKAGE_LIST_DIR/repoPkgs.list 
        docker stop $CONTAINER_ID 
        docker rm PKGNUM-$I_UUID
	else
		# get ex repo pkgs
        sed "s/REPOIDUNDERTEST/$1/g" $TEST_SCRIPTS_DIR/get-pkg-num.sh.ex > $TEST_TEMP/get-pkg-num.sh
        IMAGE_ID=$(docker images | grep $I_UUID | awk '{print $3}')
        docker create -it --name PKGNUM-$I_UUID $IMAGE_ID  /bin/bash 
        CONTAINER_ID=$(docker ps -a | grep $I_UUID | awk '{print $1}')
        docker cp $TEST_TEMP/get-pkg-num.sh $CONTAINER_ID:/root
        docker start $CONTAINER_ID 
        docker exec -it $CONTAINER_ID /bin/bash /root/get-pkg-num.sh
        docker cp $CONTAINER_ID:/root/repoPkgs.list $PACKAGE_LIST_DIR/repoPkgs.list 
        docker stop $CONTAINER_ID 
        docker rm PKGNUM-$I_UUID 
	fi
}

# 切分软件包列表
function spRepoList(){
	# 机器人总数
	sutNum=$1

	# 被测软件包总数
	pkgTotal=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`

	# 机器人总数+1
	sutNum1=`expr $sutNum + 1`

	# 每个机器人分到的包
	pkgSut=`expr $pkgTotal / $sutNum`

	# 剩余未分配的包数目
	pkgR=`expr $pkgTotal % $sutNum`

	# 已经分配的包总数
	pkgF=`expr $pkgTotal - $pkgR`

	# 剩余未分配的包列表
	cat $PACKAGE_LIST_DIR/repoPkgs.list | head -n $pkgTotal | tail -n +$[pkgF + 1] > $PACKAGE_LIST_DIR/repoPkgs-R.list

	# 切分软件包列表
	num=1	
	while [ 1 = 1 ]; do
		if [ $num -eq 1 ];then
			cat $PACKAGE_LIST_DIR/repoPkgs.list | head -n $pkgSut | tail -n +1 > $PACKAGE_LIST_DIR/repoPkgs-$num.list
		elif [ $num = $sutNum ];then
			cat $PACKAGE_LIST_DIR/repoPkgs.list | head -n $pkgF | tail -n +$temp > $PACKAGE_LIST_DIR/repoPkgs-$num.list	
		else
			if [ $num = $sutNum1  ];then
				break;
			fi
			cat $PACKAGE_LIST_DIR/repoPkgs.list | head -n $temp1 | tail -n +$temp > $PACKAGE_LIST_DIR/repoPkgs-$num.list	
		fi
		temp=$[pkgSut * num + 1]
		num=$[num + 1 ]
		temp1=$[pkgSut * num]
	done

	# 将剩余的包分配出去
	num1=1
	for temp2 in `cat $PACKAGE_LIST_DIR/repoPkgs-R.list`
	do
		echo $temp2 >> $PACKAGE_LIST_DIR/repoPkgs-$num1.list
		num1=$[num1 + 1]
	done
}

# 创建测试脚本
function createTestScripts(){
	sutNum=$1
	IU=$2
	repoid=$3
	case $2 in
		-I )
			if [ -z $3 ]
			then
				# test base repo install
				cp -p $TEST_SCRIPTS_DIR/pkg-test.sh.install $TEST_TEMP/pkg-test.sh
				runTestSingleI $1
			else
				# test ex repo install
				sed "s/REPOIDUNDERTEST/$3/g" $TEST_SCRIPTS_DIR/pkg-test.sh.exinstall > $TEST_SCRIPTS_DIR/pkg-tt.sh
				mv $TEST_SCRIPTS_DIR/pkg-tt.sh $TEST_SCRIPTS_DIR/pkg-test.sh 
				mv $TEST_SCRIPTS_DIR/pkg-test.sh $TEST_TEMP/pkg-test.sh
				runTestSingleI $1
			fi	
			;;
		-IU )
			if [ -z $3 ]
			then
				# test base repo install uninstall
				cp -p $TEST_SCRIPTS_DIR/pkg-test.sh.exiu $TEST_TEMP/pkg-test.sh
				runTestSingleIU $1
			else
				# test ex repo install uninstall
				sed "s/REPOIDUNDERTEST/$3/g" $TEST_SCRIPTS_DIR/pkg-test.sh.exiu > $TEST_SCRIPTS_DIR/pkg-tt.sh
				mv $TEST_SCRIPTS_DIR/pkg-tt.sh $TEST_SCRIPTS_DIR/pkg-test.sh 
				mv $TEST_SCRIPTS_DIR/pkg-test.sh $TEST_TEMP/pkg-test.sh
				runTestSingleIU $1
			fi	
			;; 
	esac
}

# 创建单独机器人软件包安装卸载测试脚本
function runTestSingleIU(){
	ROBOT_NUM=1
	sutNum=$1
	while [ 1 = 1 ]; do
		if [ -f $TEST_TEMP/run-test-all.sh ]
		then
			echo "*****************************************************************************" > /dev/null
		else
			cp -vp $SCRIPTS_DIR/run-test-all-template.sh $TEST_TEMP/run-test-all.sh > /dev/null
		fi
		sed "s/ROBOTNUM/$ROBOT_NUM/g" $SCRIPTS_DIR/runTestSingleIU-template.sh > $TEST_TEMP/run-test-single-$ROBOT_NUM.sh
		echo "" >> $TEST_TEMP/run-test-all.sh  
		echo "nohup bash \$TEST_TEMP/run-test-single-$ROBOT_NUM.sh > \$TEST_LOG_DIR/test-status-$ROBOT_NUM 2>&1 &" >> $TEST_TEMP/run-test-all.sh 
		echo "echo -e \"\033[42;37m 机器人 $ROBOT_NUM 已经开始执行测试 \033[0m\"" >> $TEST_TEMP/run-test-all.sh  
		echo "" >> $TEST_TEMP/run-test-all.sh  
		ROBOT_NUM=$[ $ROBOT_NUM + 1 ]
		if [ $ROBOT_NUM -gt $sutNum ]
		then
			break;
		fi
	done
}

# 创建单独机器人软件包安装测试脚本
function runTestSingleI(){
	ROBOT_NUM=1
	sutNum=$1
	while [ 1 = 1 ]; do
		if [ -f $TEST_TEMP/run-test-all.sh ]
		then
			echo "*****************************************************************************" > /dev/null
		else
			cp -vp $SCRIPTS_DIR/run-test-all-template.sh $TEST_TEMP/run-test-all.sh > /dev/null
		fi
		sed "s/ROBOTNUM/$ROBOT_NUM/g" $SCRIPTS_DIR/runTestSingleI-template.sh > $TEST_TEMP/run-test-single-$ROBOT_NUM.sh
		echo "" >> $TEST_TEMP/run-test-all.sh  
		echo "nohup bash \$TEST_TEMP/run-test-single-$ROBOT_NUM.sh > \$TEST_LOG_DIR/test-status-$ROBOT_NUM 2>&1 &" >> $TEST_TEMP/run-test-all.sh 
		echo "echo -e \"\033[42;37m 机器人 $ROBOT_NUM 已经开始执行测试 \033[0m\"" >> $TEST_TEMP/run-test-all.sh  
		echo "" >> $TEST_TEMP/run-test-all.sh  
		ROBOT_NUM=$[ $ROBOT_NUM + 1 ]
		if [ $ROBOT_NUM -gt $sutNum ]
		then
			break;
		fi
	done
}

# 进度百分比函数
function percent() {
    i=0
    j='#'
    k=('|' '\' '-' '/')
    l=0

	# 已测试软件包百分比的分子
    flag=$1
    item=1
    while [ $i -le 100 ]
    do
        printf "PROGRESS:[%-100s][%d%%][%c]\r" $j $(($i*$item)) ${k[$l]}
        j+='#'
        let i++
        let l=i%4
        sleep 0.01
        # echo $i
        if [ $i -gt $flag ]
        then
            break
        fi
    done
    printf "\n"
}

# 查看总测试进度
function testStatusAll(){
	rm -rf $TEST_TEMP/calc
	# 测试机器人数量
	ROBOT_NUM=`ls -lah $TEST_TEMP | grep robot | wc -l`
	mkdir $TEST_TEMP/calc
	# 软件包总数
	PKG_ALL=`cat $PACKAGE_LIST_DIR/repoPkgs.list | wc -l`
	i=1
	while [ 1 = 1 ]; do
		cat $TEST_TEMP/robot-$i/install.list >> $TEST_TEMP/calc/total-tested.list
		if [ -f $TEST_TEMP/robot-$i/total-install_error.list ]
		then
			cat $TEST_TEMP/robot-$i/total-install_error.list >> $TEST_TEMP/calc/total-i-e.list
		fi
		cat $TEST_TEMP/robot-$i/total-install_pass.list >> $TEST_TEMP/calc/total-i-p.list

		if [ -f $TEST_TEMP/robot-$i/total-uninstall_pass.list ]
		then
			cat $TEST_TEMP/robot-$i/total-uninstall_pass.list >> $TEST_TEMP/calc/total-u-p.list
		fi
		if [ -f $TEST_TEMP/robot-$i/total-uninstall_error.list ]
		then
			cat $TEST_TEMP/robot-$i/total-uninstall_error.list >> $TEST_TEMP/calc/total-u-e.list
		fi
		i=$[ $i + 1 ]
		if [ $i -gt $ROBOT_NUM ]
		then
			break;
		fi
	done

	echo -e "\033[45;37m 测试机器人（容器）数： $ROBOT_NUM \033[0m"
	# 已测试软件包总数
	PKG_T=`cat $TEST_TEMP/calc/total-tested.list | wc -l`
	# 到目前为止软件包安装失败的数目
	PKG_I_E=`cat $TEST_TEMP/calc/total-i-e.list | wc -l`
	# 到目前为止软件包安装成功的数目
	PKG_I_P=`cat $TEST_TEMP/calc/total-i-p.list | wc -l`
	echo -e "\033[42;37m 软件包总数 $PKG_ALL \033[0m"
	echo -e "\033[45;37m 到目前为止已测试软件包数目 $PKG_T \033[0m"
	echo -e "\033[42;37m 到目前为止软件包安装成功的数目 $PKG_I_P \033[0m"
	echo -e "\033[45;37m 到目前为止软件包安装失败的数目 $PKG_I_E \033[0m"
	if [ -f $TEST_TEMP/calc/total-u-p.list ]
	then
		# 到目前为止软件包卸载成功的数目
		PKG_U_P=`cat $TEST_TEMP/calc/total-u-p.list | wc -l`
		echo -e "\033[42;37m 到目前为止软件包卸载成功的数目 $PKG_U_P \033[0m"
	fi
	if [ -f $TEST_TEMP/calc/total-u-e.list ]
	then	
		# 到目前为止软件包卸载失败的数目
		PKG_U_E=`cat $TEST_TEMP/calc/total-u-e.list | wc -l`
		echo -e "\033[45;37m 到目前为止软件包卸载失败的数目 $PKG_U_E \033[0m"
	fi
	echo ""
	pnum=$((PKG_T*100/PKG_ALL))
    if [ $pnum -ne 100 ]
    then
        echo -e "\033[42;37m 软件包测试进度： \033[0m"
        percent $pnum
    fi

	coResults

	rm -rf $TEST_TEMP/calc
}

# 检验测试轮数
function checkTestTimes(){
	if [ -d $TEST_RESULT_DIR/test-1 ]
	then
		testTimes=`ls -lah $TEST_RESULT_DIR | grep test | wc -l`
		testTimes1=`expr $testTimes + 1`
		# echo -e "\033[42;37m 您是第 $testTImes1 次测试 \033[0m"		
		echo  $testTimes1
	else
		# mkdir $TEST_RESULT_DIR/test-1
		# echo -e "\033[42;37m 您是第一次测试 \033[0m"		
		testTimes1=1
		echo  $testTimes1
	fi
}

# 收集测试结果
function coResults(){
	DATE=`date +%Y-%m-%d-%H-%M-%S`
	times=`checkTestTimes`
	ROBOT_NUM=`ls -lah $TEST_TEMP | grep robot | wc -l`
	docker ps -a | grep $I_UUID >>/dev/null
	if [ $? -ne 0 ]
	then
		echo -e "\033[42;37m 测试完成，正在收集测试结果！！！ \033[0m"	
		mkdir $TEST_RESULT_DIR/test-$times
		i=1
		while [ 1 = 1 ]; do
			cat $TEST_TEMP/robot-$i/install.list >> $TEST_RESULT_DIR/test-$times/total-install.list
			if [ -f $TEST_TEMP/robot-$i/total-install_error.list ]
			then
				cat $TEST_TEMP/robot-$i/total-install_error.list >> $TEST_RESULT_DIR/test-$times/total-install_error.list
				cp -rvp $TEST_TEMP/robot-$i/install_error_log $TEST_RESULT_DIR/test-$times >>/dev/null
			fi
			cat $TEST_TEMP/robot-$i/total-install_pass.list >> $TEST_RESULT_DIR/test-$times/total-install_pass.list
			cp -rvp $TEST_TEMP/robot-$i/install_result $TEST_RESULT_DIR/test-$times >>/dev/null

			if [ -f $TEST_TEMP/robot-$i/uninstall.list ]
			then
				cat $TEST_TEMP/robot-$i/uninstall.list >> $TEST_RESULT_DIR/test-$times/total-uninstall.list
				cp -rvp $TEST_TEMP/robot-$i/uninstall_result $TEST_RESULT_DIR/test-$times >>/dev/null
			fi
			if [ -f $TEST_TEMP/robot-$i/total-uninstall_pass.list ]
			then
				cat $TEST_TEMP/robot-$i/total-uninstall_pass.list >> $TEST_RESULT_DIR/test-$times/total-uninstall_pass.list
			fi
			if [ -f $TEST_TEMP/robot-$i/total-uninstall_error.list ]
			then
				cat $TEST_TEMP/robot-$i/total-uninstall_error.list >> $TEST_RESULT_DIR/test-$times/total-uninstall_error.list
				cp -rvp $TEST_TEMP/robot-$i/uninstall_error_log $TEST_RESULT_DIR/test-$times >>/dev/null
			fi

			i=$[ $i + 1 ]
			if [ $i -gt $ROBOT_NUM ]
			then
				echo -e "\033[42;37m 测试结果收集完毕！！！ \033[0m"	
				echo -e "\033[42;37m 汝可以到目录 $TEST_RESULT_DIR/test-$times 中查看测试结果\033[0m"	
				break;
			fi
		done
	else
		echo -e "\033[41;37m 测试还在进行中，请勿 关闭/重启 宿主机或者操作宿主机 docker服务 ！！！ \033[0m"	
	fi
	cd $TEST_RESULT_DIR/test-$times
	tar -czvf total-error-test-$times-$DATE.tar.gz *_error.list *error_log >>/dev/null
	mv total-error-test-$times-$DATE.tar.gz $TEST_RESULT_DIR >>/dev/null
	cd $WORK_DIR
	echo -e "\033[42;37m 第 $times 次测试的测试结果 total-error-test-$times-$DATE.tar.gz 已经保存到目录$TEST_RESULT_DIR 中 \033[0m"
}

# 清除测试结果
function cleanTestResults(){
	echo -e "\033[41;37m 彻底删除之前的测试结果 ！！！ \033[0m"	
	rm -rf $TEST_RESULT_DIR/* >>/dev/null

	echo -e "\033[41;37m 彻底删除之前的测试日志 ！！！ \033[0m"	
	rm -rf $TEST_LOG_DIR/* >>/dev/null

	echo -e "\033[41;37m 彻底删除之前的测试数据 ！！！ \033[0m"	
	rm -rf $TEST_TEMP/* >>/dev/null

	echo -e "\033[41;37m 彻底删除之前的软件包列表文件 ！！！ \033[0m"	
	rm -rf $PACKAGE_LIST_DIR/* >>/dev/null
}

# 参数错误脚本
function error(){
	# 你觉得我还能添加什么新功能？
	echo -e "\033[41;37m Parameter error, you can enter parameter - h to view help information! \033[0m"
	echo -e "\033[41;37m 参数错误，可以输入参数 -h 来查看帮助信息! \033[0m"
	echo ""
	echo "Contact Author"
	echo "联系作者"
	echo "Author:	futeng"
	echo "Tel:	13439166273"
	echo "QQ:	512993560"
	echo "Weixin:	tmlwft"
	echo "Email:	lanshaa123@163.com tmlw123@hotmail.com"
	exit 0
}
